home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / modem / wsftp2.zip / WS_HOST.C < prev    next >
C/C++ Source or Header  |  1994-06-03  |  33KB  |  992 lines

  1. /***************************************************************************
  2.   Windows Sockets Client Application Support Module
  3.  
  4.   Written by:
  5.       John A. Junod             Internet: <junodj@gordon-emh2.army.mil>
  6.       267 Hillwood Street                 <zj8549@trotter.usma.edu>
  7.       Martinez, GA 30907      Compuserve: 72321,366 
  8.  
  9.   This program executable and all source code is released into the public
  10.   domain.  It would be nice (but is not required) to give me a little 
  11.   credit for any use of this code.  
  12.  
  13.   THE INFORMATION AND CODE PROVIDED IS PROVIDED AS IS WITHOUT WARRANTY 
  14.   OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  15.   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  16.   PURPOSE. IN NO EVENT SHALL JOHN A. JUNOD BE LIABLE FOR ANY DAMAGES 
  17.   WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS 
  18.   OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF JOHN A. JUNOD HAS BEEN 
  19.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  20.  
  21. *****************************************************************************/
  22.  
  23. #include "ws_glob.h"
  24. #include "ws_ftp.h"
  25. #include <io.h>     // access()
  26. #include <stdlib.h> // atoi()
  27.  
  28. char szInitDir[80];
  29. char szSessionName[80];
  30.  
  31. #define MAXTYPES 16
  32. LPSTR szHostTypes[MAXTYPES]={
  33.   "auto detect",
  34.   "Chameleon",
  35.   "FTP PC-TCP",
  36.   "IBM-PC TCP/IP",
  37.   "IBM-VM/MVS",
  38.   "KA9Q/NOS",
  39.   "NCSA/CUTCP",
  40.   "SI NT FTPD",
  41.   "Super TCP",
  42.   "Unisys 5000 (EXOS)",
  43.   "UNIX (default)",
  44.   "VMS-Multinet",
  45.   "VMS-UCX",
  46.   "WinQVT/Net",
  47.   "Windows NT",
  48.   "Solaris - Unix"
  49.   };
  50.  
  51. // int nTypes[MAXHOSTS];
  52. BOOL bSavePWD=FALSE;
  53. BOOL bUseGateWay=FALSE;
  54. char szCrypt[160];
  55. LPSTR szAnony="anonymous";
  56. int nHostType=HOST_TYPE_AUTO;
  57. extern BOOL bHELP;
  58. extern BOOL bCanMKD,bCanRMD,bCanREN,bCanDELE;
  59. extern HBRUSH hbrGray1,hbrGray2;
  60.  
  61.    // lgk new function to get remote search string
  62.  
  63.  char *remotesearchstring()
  64.   {
  65.  
  66.    char temp[110];
  67.    SendMessage(hRdirBox,WM_GETTEXT,100,(LONG)temp);
  68.    return temp;
  69.   }
  70.  
  71.  
  72. void LoadConfigNames(HWND);
  73. /*
  74. // this encryption is not secure nor is it intended to be
  75. // this is just to keep the password from being plain text
  76. // in the ini file.  I'd really recommend people don't save
  77. // their passwords
  78. */
  79. LPSTR EnCrypt(LPSTR userid,LPSTR passwd)
  80. {
  81.   int nIndex;
  82.   if(lstrcmp(userid,szAnony)==0)
  83.     return(passwd);
  84.   szCrypt[0]=0;
  85.   for(nIndex=0;nIndex<lstrlen(passwd);nIndex++) {
  86.     wsprintf(&szCrypt[nIndex*2],"%02X",
  87.       ((char)passwd[nIndex])+nIndex);
  88.   }
  89.   return(szCrypt);
  90. }
  91.  
  92. int unhex(char c) {
  93.   if(c>'9') return(c-'7');
  94.   return (c-'0');
  95. }
  96.  
  97. LPSTR DeCrypt(LPSTR userid,LPSTR passwd)
  98. {
  99.   int nIndex;
  100.   if(lstrcmp(userid,szAnony)==0)
  101.     return(passwd);
  102.   szCrypt[0]=0;
  103.   for(nIndex=0;nIndex<lstrlen(passwd);nIndex+=2) {
  104.     (BYTE)szCrypt[nIndex/2]=
  105.       ((unhex(passwd[nIndex])*16)+
  106.        unhex(passwd[nIndex+1]))-(nIndex/2);
  107.     szCrypt[nIndex/2+1]=0;
  108.   }
  109.   return(szCrypt);
  110. }
  111.  
  112. /*******************************************************
  113.   Set the dialog box controls according to the current
  114.   selected host.
  115. */
  116. SetDefaultHostStuff(HWND hWndDlg,LPSTR szSession) {
  117.  
  118.   szRemoteHost[0]=0;
  119.   GetPrivateProfileString(szSession,"HOST",szSession,szRemoteHost,79,szIniFile);
  120.   SetDlgItemText(hWndDlg,DLG_HOST_ADDRESS,szRemoteHost);
  121.  
  122.   szUserID[0]=0;
  123.   GetPrivateProfileString(szSession,"UID",NULL,szUserID,79,szIniFile);
  124.   SetDlgItemText(hWndDlg,DLG_HOST_USERID,szUserID);
  125.  
  126.   szPassWord[0]=0; szMsgBuf[0]=0; bSavePWD=FALSE;
  127.   GetPrivateProfileString(szSession,"PWD",NULL,szMsgBuf,79,szIniFile);
  128.   if(szUserID[0]!=0 && szMsgBuf[0]!=0) {
  129.     lstrcpy(szPassWord,DeCrypt(szUserID,szMsgBuf));
  130.     bSavePWD=TRUE;
  131.   }
  132.   CheckDlgButton(hWndDlg,DLG_HOST_PWD,bSavePWD);
  133.  
  134.   if(lstrcmpi(szUserID,szAnony)==0) {
  135.     CheckDlgButton(hWndDlg,DLG_HOST_ANONY,TRUE);
  136.     if(szPassWord[0]==0) {
  137.       if(szMailAddress[0]==0)
  138.         StdInput(szMailAddress,"Enter your e-mail address:");
  139.       else
  140.         lstrcpy(szPassWord,szMailAddress);
  141.     }
  142.   } else
  143.     CheckDlgButton(hWndDlg,DLG_HOST_ANONY,FALSE);
  144.   SetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szPassWord);
  145.  
  146.   szInitDir[0]=0;
  147.   GetPrivateProfileString(szSession,"DIR",NULL,szInitDir,79,szIniFile);
  148.   SetDlgItemText(hWndDlg,DLG_HOST_DIR,szInitDir);
  149.  
  150.   szGateHost[0]=0; bUseGateWay=FALSE;
  151.   GetPrivateProfileString(szSession,"GATEHOST",NULL,szGateHost,79,szIniFile);
  152.   if(szGateHost[0]!=0) bUseGateWay=TRUE;
  153.   CheckDlgButton(hWndDlg,DLG_HOST_GATEWAY,bUseGateWay);
  154.  
  155.   nHostType=GetPrivateProfileInt(szSession,"TYPE",HOST_TYPE_AUTO,szIniFile);
  156.   SendDlgItemMessage(hWndDlg,DLG_HOST_TYPE,CB_SETCURSEL,nHostType-6000,0l);
  157.  
  158.   uiTimeOut=GetPrivateProfileInt(szSession,"TIMEOUT",65,szIniFile);
  159.   SetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,uiTimeOut,FALSE);
  160.  
  161.   return TRUE;
  162. }
  163.  
  164. BOOL CALLBACK WS_GateMsgProc(HWND hWndDlg, WORD Message,
  165.                                WORD wParam, LONG lParam)
  166. {
  167.  //nt nIndex;
  168.  //INT nRC;
  169.  
  170.   switch(Message)
  171.   {
  172.     case WM_INITDIALOG:
  173.       GetPrivateProfileString(szSessionName,"GATEHOST",
  174.         NULL,szGateHost,79,szIniFile);
  175.       SetDlgItemText(hWndDlg,DLG_HOST_ADDRESS,szGateHost);
  176.       GetPrivateProfileString(szSessionName,"GATEUSERID",
  177.         NULL,szGateUserID,79,szIniFile);
  178.       SetDlgItemText(hWndDlg,DLG_HOST_USERID,szGateUserID);
  179.       if(GetPrivateProfileString(szSessionName,"GATEPASSWORD",
  180.         NULL,szGatePassWord,79,szIniFile))
  181.         strcpy(szGatePassWord,DeCrypt(szGateUserID,szGatePassWord));
  182.       SetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szGatePassWord);
  183.       CheckDlgButton(hWndDlg,DLG_HOST_PWD,bSavePWD);
  184.       break;
  185.     case WM_CLOSE:
  186.       PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  187.       break;
  188.  
  189.       
  190.         case WM_CTLCOLORBTN:
  191.             if(LOWORD(lParam)<10) return((LRESULT)NULL);
  192.        case WM_CTLCOLORDLG:
  193.         case WM_CTLCOLORSTATIC:
  194.           SetBkColor((HDC) wParam, RGB(192,192,192));
  195.           return(LRESULT)hbrGray1;
  196.           break;
  197.  
  198.         case WM_CTLCOLORLISTBOX:
  199.           return(LRESULT)hbrGray1;
  200.           break;
  201.  
  202.     case WM_COMMAND:
  203.       switch(LOWORD(wParam))
  204.       {
  205.         case IDOK:
  206.           GetDlgItemText(hWndDlg,DLG_HOST_ADDRESS,szGateHost,79);
  207.           WritePrivateProfileString(szSessionName,"GATEHOST",
  208.              szGateHost,szIniFile);
  209.           GetDlgItemText(hWndDlg,DLG_HOST_USERID,szGateUserID,79);
  210.           WritePrivateProfileString(szSessionName,"GATEUSERID",
  211.              szGateUserID,szIniFile);
  212.           GetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szGatePassWord,79);
  213.           if(IsDlgButtonChecked(hWndDlg,DLG_HOST_PWD))
  214.             WritePrivateProfileString(szSessionName,"GATEPASSWORD",
  215.               EnCrypt(szGateUserID,szGatePassWord),szIniFile);
  216.           else
  217.             WritePrivateProfileString(szSessionName,"GATEPASSWORD",
  218.               NULL,szIniFile);
  219.           EndDialog(hWndDlg,TRUE);
  220.           break;
  221.         case IDCANCEL:
  222.           EndDialog(hWndDlg, FALSE);
  223.           break;
  224.       }
  225.       break;
  226.     default:
  227.       return FALSE;
  228.   }
  229.   return TRUE;
  230. }
  231.  
  232. /******************************************************************
  233.   Message processing for host dialog box
  234. */
  235. BOOL CALLBACK WS_HostMsgProc(HWND hWndDlg, UINT Message,
  236.                                WPARAM wParam, LPARAM lParam)
  237. {
  238.   int nIndex;
  239.  //char szRHost[80];
  240.   UINT nRC;
  241.  
  242.   switch(Message)
  243.   {
  244.     case WM_INITDIALOG:
  245.       LoadConfigNames(hWndDlg);
  246.       for(nIndex=0;nIndex<MAXTYPES;nIndex++)
  247.         SendDlgItemMessage(hWndDlg,DLG_HOST_TYPE,CB_ADDSTRING,
  248.           0,(LONG)(LPSTR)szHostTypes[nIndex]);
  249.       SetDlgItemText(hWndDlg,DLG_HOST_NAME,szSessionName);
  250.       SetDlgItemText(hWndDlg,DLG_HOST_ADDRESS,szRemoteHost);
  251.       SetDlgItemText(hWndDlg,DLG_HOST_USERID,szUserID);
  252.       SetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szPassWord);
  253.       SetDlgItemText(hWndDlg,DLG_HOST_DIR,szInitDir);
  254.       SetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,uiTimeOut,FALSE);
  255.       CheckDlgButton(hWndDlg,DLG_HOST_PWD,bSavePWD);
  256.       SendDlgItemMessage(hWndDlg,DLG_HOST_TYPE,CB_SETCURSEL,nHostType-6000,0L);
  257.       if(szSessionName[0]==0)
  258.         strcpy(szSessionName,szRemoteHost);
  259.       SetDefaultHostStuff(hWndDlg,szSessionName);
  260.       cwCenter(hWndDlg, 0);
  261.       SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,WM_SETREDRAW,TRUE,0l);
  262.       break;
  263.     case WM_CLOSE:
  264.       PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  265.       break;
  266.  
  267.  
  268.      case WM_CTLCOLORBTN:
  269.            if(LOWORD(lParam)<10) return((LRESULT)NULL);
  270.      case WM_CTLCOLORDLG:
  271.      case WM_CTLCOLORSTATIC:
  272.            SetBkColor((HDC) wParam, RGB(192,192,192));
  273.           return (LRESULT)hbrGray1;
  274.            break;
  275.  
  276.        case WM_CTLCOLORLISTBOX:
  277.          return(LRESULT)hbrGray1;
  278.          break;
  279.  
  280. /*    case WM_CTLCOLOR:
  281.       switch(HIWORD(lParam)) {
  282.         case CTLCOLOR_BTN:
  283.           if(LOWORD(lParam)<10) return((LRESULT)NULL);
  284.         case CTLCOLOR_DLG:
  285.         case CTLCOLOR_STATIC:
  286.           SetBkColor((HDC) wParam, RGB(192,192,192));
  287.         case CTLCOLOR_LISTBOX:
  288.           return(LRESULT)hbrGray1;
  289.         } 
  290.  
  291.         return(LRESULT)NULL;
  292.         break; */
  293.  
  294.             // lgk change hiword lparam to loword wparam
  295.     case WM_COMMAND:
  296.       switch(LOWORD(wParam))
  297.       {
  298.         case DLG_HOST_NAME:
  299.           if(HIWORD(wParam)==CBN_KILLFOCUS ||
  300.              HIWORD(wParam)==CBN_EDITCHANGE) 
  301.              {
  302.               GetDlgItemText(hWndDlg,DLG_HOST_NAME,szSessionName,79);
  303.              } 
  304.            else if(HIWORD(wParam)==CBN_SELCHANGE) 
  305.               {
  306.                 if((nIndex=SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,
  307.                           CB_GETCURSEL,0,0L))!=LB_ERR)
  308.                    SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_GETLBTEXT,
  309.                                     nIndex,(LONG)szSessionName);
  310.                 else break;
  311.  
  312.               } 
  313.            else break;
  314.           SetDefaultHostStuff(hWndDlg,szSessionName);
  315.  
  316. /*                   SetDefaultHostStuff(hWndDlg,szSessionName);
  317.  
  318.           if(HIWORD(lParam)==CBN_KILLFOCUS)
  319.             SendDlgItemMessage(hWndDlg,DLG_HOST_ADDRESS,WM_SETREDRAW,TRUE,0l);
  320. */
  321.           break;
  322.  
  323.         case DLG_HOST_ANONY:
  324.           if(IsDlgButtonChecked(hWndDlg,DLG_HOST_ANONY)) {
  325.             SetDlgItemText(hWndDlg,DLG_HOST_USERID,szAnony);
  326.             if(*szMailAddress==0)
  327.               StdInput(szMailAddress,"Enter your e-mail address:");
  328.             SetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szMailAddress);
  329.             SetFocus(GetDlgItem(hWndDlg,DLG_HOST_ANONY));
  330.           }
  331.           return(FALSE);
  332.  
  333.         case DLG_BTN_DEL:
  334.           if(GetDlgItemText(hWndDlg,DLG_HOST_NAME,szSessionName,79)>0) {
  335.             WritePrivateProfileString(szSessionName,NULL,NULL,szIniFile);
  336.             if((nIndex=SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,
  337.                CB_FINDSTRINGEXACT,0,(LONG)(LPSTR)szSessionName))!=CB_ERR) {
  338.               SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_DELETESTRING,
  339.                  nIndex,0l);
  340.             } else nIndex=0;
  341.             if(SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_SETCURSEL,nIndex,0l)
  342.               ==CB_ERR)
  343.               SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_SETCURSEL,0,0l);
  344.           }
  345.           GetDlgItemText(hWndDlg,DLG_HOST_NAME,szSessionName,79);
  346.           SetDefaultHostStuff(hWndDlg,szSessionName);
  347.           SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,WM_SETREDRAW,TRUE,0l);
  348.           SetFocus(GetDlgItem(hWndDlg,DLG_HOST_NAME));
  349.           break;
  350.  
  351.         case DLG_BTN_SAVE:
  352.         case IDOK:
  353.           GetDlgItemText(hWndDlg,DLG_HOST_ADDRESS,szRemoteHost,79);
  354.           if(GetDlgItemText(hWndDlg,DLG_HOST_NAME,szSessionName,79)==0)
  355.             lstrcpy(szSessionName,szRemoteHost);
  356.           GetDlgItemText(hWndDlg,DLG_HOST_USERID,szUserID,79);
  357.           GetDlgItemText(hWndDlg,DLG_HOST_PASSWORD,szPassWord,79);
  358.           GetDlgItemText(hWndDlg,DLG_HOST_DIR,szInitDir,79);
  359.           nRC=GetDlgItemInt(hWndDlg,DLG_HOST_TIMEOUT,NULL,FALSE);
  360.           if(nRC==0) uiTimeOut=65; else uiTimeOut=nRC;
  361.           if(uiTimeOut > (65536/1000)) uiTimeOut=65530/1000;
  362.           if(IsDlgButtonChecked(hWndDlg,DLG_HOST_PWD))
  363.             bSavePWD=TRUE;
  364.           else
  365.             bSavePWD=FALSE;
  366.           nIndex=SendDlgItemMessage(hWndDlg,DLG_HOST_TYPE,CB_GETCURSEL,0,0l);
  367.           if(nIndex==CB_ERR)
  368.             nHostType=HOST_TYPE_AUTO;
  369.           else
  370.             nHostType=nIndex+HOST_TYPE_AUTO;
  371.           SaveHostName(szSessionName);
  372.           if (IsDlgButtonChecked(hWndDlg, DLG_HOST_GATEWAY))
  373.             { FARPROC lpfnMsgProc;
  374.               use_gateway=1;
  375.               lpfnMsgProc=MakeProcInstance((FARPROC)WS_GateMsgProc,hInst);
  376.               nRC=DialogBox(hInst,(LPSTR)"DLG_GATEWAY",hWndDlg,lpfnMsgProc);
  377.               FreeProcInstance(lpfnMsgProc);
  378.               SetFocus(hWndDlg);
  379.             } else use_gateway=0;
  380.           if(LOWORD(wParam)==IDOK)
  381.             EndDialog(hWndDlg, TRUE);
  382.           else
  383.             if(SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,
  384.                CB_FINDSTRINGEXACT,0,(LONG)(LPSTR)szSessionName)==CB_ERR) {
  385.               SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_ADDSTRING,
  386.                  0,(LONG)(LPSTR)szSessionName);
  387.               SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,WM_SETREDRAW,TRUE,0l);
  388.               SetFocus(GetDlgItem(hWndDlg,DLG_HOST_NAME));
  389.             }
  390.           break;
  391.  
  392.         case IDCANCEL:
  393.           EndDialog(hWndDlg, FALSE);
  394.  
  395.          if(!bConnected)
  396.            {
  397.            EnableWindow(hBtnConnect,TRUE);
  398.            EnableWindow(hBtnClose,FALSE);
  399.            }
  400.           break;
  401.       }
  402.       break;
  403.     default:
  404.       return FALSE;
  405.   }
  406.   return TRUE;
  407. }
  408.  
  409. /***************************************************************
  410.   save the information about the current host in the ini file
  411. */
  412. void SaveHostName(LPSTR lpszCName)
  413. {
  414.   char buf[30];
  415.   WritePrivateProfileString(lpszCName,"HOST",szRemoteHost,szIniFile);
  416.   WritePrivateProfileString(lpszCName,"UID",szUserID,szIniFile);
  417.   if(bSavePWD)
  418.     WritePrivateProfileString(lpszCName,"PWD",
  419.         EnCrypt(szUserID,szPassWord),szIniFile);
  420.   else
  421.     WritePrivateProfileString(lpszCName,"PWD",NULL,szIniFile);
  422.   if(szInitDir[0]!=0)
  423.     WritePrivateProfileString(lpszCName,"DIR",szInitDir,szIniFile);
  424.   else
  425.     WritePrivateProfileString(lpszCName,"DIR",NULL,szIniFile);
  426.   wsprintf(buf,"%u",nHostType);
  427.   WritePrivateProfileString(lpszCName,"TYPE",buf,szIniFile);
  428.   wsprintf(buf,"%u",uiTimeOut);
  429.   WritePrivateProfileString(lpszCName,"TIMEOUT",buf,szIniFile);
  430. }
  431.  
  432. /*****************************************************
  433.   convert remotename into something DOS can deal with here nt can deal with longer names
  434. */
  435. // lgk add fix for vax which makes names with ;version numbe now
  436.  
  437. MakeLocalName(LPSTR localname,LPSTR shortname, LPSTR remotename)
  438. {
  439.   int nIndex;
  440.   char Name[10],Ext[4],*s;
  441.   char Name2[140], Ext2[80];
  442.   char savedname[256];
  443.  
  444.   lstrcpy(savedname,remotename);
  445.  
  446.   // lgk on nt try the entire name first for localname
  447.  
  448.   while(*remotename!=0 && *remotename=='.')
  449.     remotename++;
  450.   for(nIndex=0;nIndex<140;nIndex++)
  451.     if(*remotename!=0 && *remotename!='.' && *remotename!=' ')
  452.       { 
  453.         if (nIndex < 8)
  454.          Name[nIndex] = *remotename;
  455.         Name2[nIndex] = *remotename++;
  456.       }
  457.     else break;
  458.  
  459.  if (nIndex <= 8)
  460.    Name[nIndex]=0; 
  461.  else Name[8]=0; 
  462.   
  463.   Name2[nIndex]=0; 
  464.   Ext2[0]=0;
  465.   Ext[0] = 0;
  466.  
  467.   if((s=strchr(remotename,'.'))!=NULL)
  468.     remotename=s;
  469.   while(*remotename!=0 && (*remotename=='.' || *remotename==' '))
  470.     remotename++;
  471.   if(*remotename!=0) {
  472.     for(nIndex=0;nIndex<80;nIndex++)
  473.       if(*remotename!=0 && *remotename!='.' && *remotename!=' ')
  474.         { if (nIndex < 3) 
  475.             Ext[nIndex] = *remotename;
  476.           Ext2[nIndex] = *remotename++;
  477.         }
  478.       else break;
  479.  
  480.   if (nIndex <=3)
  481.    Ext[nIndex]=0;
  482.   else Ext[3] = 0;
  483.  
  484.     Ext2[nIndex] = 0;
  485.   }
  486.   
  487.   if(Ext[0]==0) {
  488.     lstrcpy(shortname,Name);
  489.   } else {
  490.     wsprintf(shortname,"%s.%s",Name,Ext);
  491.   }
  492.  if(Ext2[0]==0) {
  493.     lstrcpy(localname,Name2);
  494.   } else {
  495.     wsprintf(localname,"%s.%s",Name2,Ext);
  496.   }
  497.  
  498.   if(lstrlen(shortname)==0) {
  499.     lstrcpy(Name,"aaremote");
  500.     lstrcpy(shortname,Name);
  501.   }
  502.   if(lstrlen(localname)==0) {
  503.     lstrcpy(Name,"aaremote");
  504.     lstrcpy(localname,Name2);
  505.   }
  506.  
  507.   if(bRecvUniq) {
  508.     nIndex=0;
  509.     while((int)access(shortname,0)==0 && nIndex<99) {
  510.       DoPrintf("[recvuniq] %s - %s - %s",Name,Ext,shortname);
  511.       if(Ext[0]==0)
  512.         wsprintf(shortname,"%s.%03u",Name,nIndex);
  513.       else if(lstrlen(Name)>5)
  514.         wsprintf(shortname,"%-5.5s%03u.%s",Name,nIndex,Ext);
  515.       else wsprintf(shortname,"%s%03u.%s",Name,nIndex,Ext);
  516.       nIndex++;
  517.     }
  518.   }
  519.   
  520.  if(bRecvUniq) {
  521.     nIndex=0;
  522.     while((int)access(localname,0)==0 && nIndex<99) {
  523.       DoPrintf("[recvuniq] %s - %s - %s",Name2,Ext2,localname);
  524.       if(Ext[0]==0)
  525.         wsprintf(localname,"%s.%03u",Name2,nIndex);
  526.       else if(lstrlen(Name)>5)
  527.         wsprintf(localname,"%-5.5s%03u.%s",Name2,nIndex,Ext2);
  528.       else wsprintf(localname,"%s%03u.%s",Name2,nIndex,Ext2);
  529.       nIndex++;
  530.     }
  531.   }
  532.    
  533.   // on nt try whole name for local name
  534.   lstrcpy(localname,savedname);
  535.   // fix vax however
  536.   {
  537.     char  *colonloc = strchr(savedname,';');
  538.     
  539.   if (colonloc != 0)
  540.     {
  541.       int i = 0;
  542.       while (savedname[i] != ';')
  543.         {
  544.          Name2[i] = savedname[i];
  545.          ++i;
  546.         }
  547.      Name2[i] = '\0';
  548.      lstrcpy(localname,Name2);
  549.     }
  550.   }  
  551.   return(TRUE);
  552. }
  553.  
  554. /*************************************************
  555.   find filename in a UNIX directory listing
  556. */
  557. LPSTR FindName(LPSTR szLine)
  558. {
  559.   int nIndex;
  560.   char *pStr;
  561.  
  562.   // strip trailing garbage from the line if there is any.
  563.   while((nIndex=strlen(szLine))>2 &&
  564.     (szLine[nIndex-1]==0x0a || szLine[nIndex-1]==0x0d || 
  565.      szLine[nIndex-1]==' ' || szLine[nIndex-1]==0x09))
  566.     szLine[nIndex]=0;
  567.  
  568.   // now the name SHOULD be the last thing on the line
  569.   if((pStr=strrchr(szLine,' '))!=NULL ||
  570.      (pStr=strrchr(szLine,0x09))!=NULL) {
  571.     while(*pStr && (*pStr==' ' || *pStr==0x09)) pStr++;
  572.     return(pStr);
  573.   }
  574.   return(szLine);
  575. }
  576.  
  577. /*****************************************************************
  578.   This is the routine that take the output from LIST and breaks
  579.   it down into files and directories.  The format for the output
  580.   from most of the machine types was provided by Chris Sacksteder.
  581. */
  582. int GetRemoteDirForWnd(HWND hWnd)
  583. {
  584.   char *pStr,*s,*t;
  585.   FILE *fd;
  586.   int result = 0;
  587.   int nRC;
  588.   char *searchstring = remotesearchstring();
  589.   
  590.   char temp[120];
  591.  
  592.   strcpy(temp,searchstring);
  593.  
  594.   // clean out the old contents of the list boxes
  595.   SendMessage(hLbxRDir,LB_RESETCONTENT,0,0);
  596.   SendMessage(hLbxRFiles,LB_RESETCONTENT,0,0);
  597.   // can't do much if we aren't connected
  598.   if(!bConnected) {
  599.     SendMessage(hTxtRDir,WM_SETTEXT,0,(LPARAM)"");
  600.   }
  601.   else {
  602.     // get the remote directory name
  603.     strcpy(szString,"undecipherable");
  604.     nRC=DoPWD(ctrl_socket);
  605.     if(nRC==FTP_COMPLETE) {
  606.       if((pStr=strchr(szMsgBuf,'"'))!=NULL)
  607.         strncpy(szString,++pStr,180);
  608.       if((pStr=strchr(szString,'"'))!=NULL)
  609.         *pStr=0;
  610.       else szString[180]=0;
  611.     }
  612.     SendMessage(hTxtRDir,WM_SETTEXT,0,(LPARAM)szString);
  613.  
  614.     if(!bHELP) ReadProcessHelp(ctrl_socket);
  615.     // go get the current remote directory listing in tmpfile.tmp
  616.    
  617.    // lgk new code here to use search string if filled in
  618.     if (strlen(temp) != 0)
  619.       {
  620.        char temp2[200];
  621.        strcpy(temp2,"LIST ");
  622.        strcat(temp2,temp);
  623.        nRC=DoDirList(ctrl_socket,temp2);
  624.       }
  625.     else nRC=DoDirList(ctrl_socket,"LIST");
  626.  
  627.     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LPARAM)"..");
  628.     if(nRC==FTP_COMPLETE) {
  629.       if((fd=fopen(szTmpFile,"r"))!=NULL) {
  630.         while(fgets(szString,180,fd)!=NULL) {
  631.           if((pStr=strchr(szString,'\n'))!=NULL) *pStr=0;
  632.           switch(nHostType) {
  633.             case HOST_TYPE_SUPER:
  634.             case HOST_TYPE_CHAMELEON:
  635.             case HOST_TYPE_NCSA:
  636.             // looks like a standard DOS directory
  637.                 //  filename.ext       <DIR>(or size)  date,etc...
  638.                 if(strstr(szString,"<DIR>")!=NULL || 
  639.                    strstr(szString,"<dir>")!=NULL) {
  640.                   if((pStr=strchr(szString,' '))!=NULL) *pStr=0;
  641.                   if(strcmp(szString,".")!=0 && strcmp(szString,"..")!=0)
  642.                     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  643.                 } else {
  644.                   if((pStr=strchr(szString,' '))!=NULL) *pStr=0;
  645.                   if(szString[0]!=0)
  646.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  647.                 }
  648.                 break;
  649.           case HOST_TYPE_WINDOWS_NT:
  650.                   if(strstr(szString,"<DIR>")!=NULL) 
  651.                    {
  652.                     if ((pStr = strstr(szString,"<DIR>")) != NULL)
  653.                         if ((pStr = strrchr(pStr,' ')) != NULL)
  654.                           {
  655.                            /* now we should be on name so get it */
  656.                            ++pStr;
  657.                            if(strcmp(pStr,".")!=0 && strcmp(pStr,"..")!=0)
  658.                            SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)pStr);
  659.                           }
  660.                    }
  661.                 
  662.                 else 
  663.                  {
  664.                  /* get to the name     */
  665.                    if ((pStr = strrchr(szString,' ')) != NULL)
  666.                        {
  667.                          ++pStr;
  668.                          /* Now we have the name */
  669.                           SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)pStr);
  670.                        }
  671.                 }
  672.                 break;
  673.                 
  674.                 case HOST_TYPE_QVT:
  675.             // if the name is the first thing on the line and ends with a slash
  676.             // to indicate a directory
  677.                 if((s=strchr(szString,' '))!=NULL) *s=0; // use for ftp.uwp.edu
  678.                 if(szString[strlen(szString)-1]=='/' || 
  679.                    szString[strlen(szString)-1]=='\\')
  680.                 {
  681.                   szString[strlen(szString)-1]=0;
  682.                   if(strcmp(szString,".")!=0 && strcmp(szString,"..")!=0)
  683.                     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  684.                 } else
  685.                   SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  686.                 break;
  687.             case HOST_TYPE_IBM_VM:
  688.             // IBM VM/VMS listing
  689.                 // filename extent (what does a directory look like??)
  690.                 szString[18]=0;
  691.                 while(strlen(szString)>1 && szString[strlen(szString)-1]==' ')
  692.                   szString[strlen(szString)-1]=0;
  693.                 if((s=strchr(szString,' '))!=NULL) {
  694.                   *s++ = '.';
  695.                   t=s;
  696.                   while(*t && *t==' ') t++;
  697.                   if(*t && t!=s) strcpy(s,t);
  698.                 }
  699.                 if(*s && *s!=' ' && *s!='\t')  // 93.12.04 eliminate blank lines
  700.                   SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  701.                 break;
  702.             case HOST_TYPE_VMS_UCX:
  703.             case HOST_TYPE_VMS_MULTINET:
  704.             // DEC listing
  705.                 if(*szString!=' ') {
  706.                   if((s=strchr(szString,';'))!=NULL) {
  707.                     *s=0;
  708.                     if(strlen(szString)>4 &&
  709.                        strcmp(&szString[strlen(szString)-4],".DIR")==0) {
  710.                       szString[strlen(szString)-4]=0;
  711.                       SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  712.                     } else {
  713.                       *s=';';
  714.                       if((s=strchr(szString,' '))!=NULL) *s=0;
  715.                       if((s=strchr(szString,'\t'))!=NULL) *s=0;
  716.                       SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  717.                     }
  718.                   }
  719.                 }
  720.                 break;
  721.             case HOST_TYPE_PCTCP:
  722.             // size..... filename.ext   .......
  723.             // <dir>     filename.ext   .......
  724.                 szString[30]=0;
  725.                 s=FindName(szString);
  726.                 if(strnicmp(szString,"<dir>",5)==0) {
  727.                   if(strcmp(s,".")!=0 && strcmp(s,"..")!=0)
  728.                     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)s);
  729.                 } else
  730.                   SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)s);
  731.                 break;
  732.             case HOST_TYPE_IBM_TCP:
  733.             // ........   <dir>   filename.ext
  734.             // ........    DIR    filename.ext    - host ibm tcp
  735.             // ........   size    filename.ext
  736.                 s=FindName(szString);
  737.                 if(strstr(szString," DIR ")!=NULL || 
  738.                    strstr(szString,"<dir>")!=NULL) {
  739.                   if(strcmp(s,".")!=0 && strcmp(s,"..")!=0)
  740.                     SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)s);
  741.                 } else
  742.                   SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)s);
  743.                 break;            
  744.             case HOST_TYPE_NOS:
  745.             // ka9q 
  746.               if(strstr(szString,"Disk size")==NULL) {
  747.                 szString[13]=0; nRC=13;
  748.                 while((nRC=strlen(szString))>0 && szString[nRC-1]==' ')
  749.                   szString[nRC-1]=0;
  750.                 if(*szString!=0) {
  751.                   if(szString[strlen(szString)-1]=='/' || 
  752.                      szString[strlen(szString)-1]=='\\')
  753.                   {
  754.                     szString[strlen(szString)-1]=0;
  755.                     if(strcmp(szString,".")!=0)
  756.                       SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)szString);
  757.                   } else
  758.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)szString);
  759.                 }
  760.                 pStr=&szString[41]; nRC=13; pStr[nRC]=0;
  761.                 while((nRC=strlen(pStr))>0 && pStr[nRC-1]==' ')
  762.                   pStr[nRC-1]=0;
  763.                 if(*pStr!=0) {
  764.                   if(pStr[strlen(pStr)-1]=='/' ||
  765.                      pStr[strlen(pStr)-1]=='\\')
  766.                   {
  767.                     pStr[strlen(pStr)-1]=0;
  768.                     if(strcmp(pStr,".")!=0 && strcmp(pStr,"..")!=0)
  769.                       SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)pStr);
  770.                   } else
  771.                     SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)pStr);
  772.                 }
  773.               }
  774.               break;
  775.             case HOST_TYPE_SINTFTPD:
  776.             case HOST_TYPE_U5000:
  777.             case HOST_TYPE_UNIX:
  778.             case HOST_TYPE_SOLARIS:
  779.             default:
  780.             // assume UNIX ls format
  781.               pStr=FindName(szString);
  782.               // if line starts with 'd' its a directory
  783.               if(strchr("dl",szString[0])!=NULL) {
  784.                 if(pStr[strlen(pStr)-1]=='/' || // 93.12.04 fix for some NT ftpd
  785.                    pStr[strlen(pStr)-1]=='\\')  // 93.12.04
  786.                   pStr[strlen(pStr)-1]=0;       // 93.12.04
  787.                 if(strcmp(pStr,".")!=0 && strcmp(pStr,"..")!=0)
  788.                   SendMessage(hLbxRDir,LB_ADDSTRING,0,(LONG)pStr);
  789.               } else
  790.               // if line starts with - or f its a file
  791.               if(nHostType==HOST_TYPE_SINTFTPD ||
  792.                  strchr("-f",szString[0])!=NULL) {
  793.                 SendMessage(hLbxRFiles,LB_ADDSTRING,0,(LONG)pStr);
  794.               }
  795.               break;
  796.           }
  797.         }
  798.         fclose(fd);
  799.       } else
  800.         DoAddLine("couldn't open tmpfile for read.");
  801.     } else
  802.       DoPrintf("DoDirList returned %u",nRC);
  803.   } // if we were connected
  804.   SendMessage(hRdirBox,EM_SETMODIFY,0,0);
  805.   return 0;
  806. }
  807.  
  808. /*
  809.   Read the output from HELP and see if we can determine the hosttype
  810.   (if we don't already know it) and determine what functions are
  811.   valid (things like mkdir, etc)
  812. lgk bug fix for solaris where remotehelp hangs the system.
  813. */
  814. int ReadProcessHelp(SOCKET ctrl_socket)
  815. {
  816.   int iRetCode;
  817.  
  818.   // lgk add support for getting the other types by executing the syst command
  819.  
  820.   BOOL defaulttype = FALSE;
  821.  
  822.   bCanMKD=bCanRMD=bCanREN=bCanDELE=FALSE;
  823.  
  824.   // lgk don't send help for solaris
  825.   if (nHostType != HOST_TYPE_SOLARIS)
  826.    {
  827.     
  828.   if(SendPacket(ctrl_socket,"HELP")!=-1) 
  829.    {
  830.     iRetCode=ReadLine(ctrl_socket);
  831.     if((iRetCode/100)==5 && nHostType==HOST_TYPE_AUTO) nHostType=HOST_TYPE_NOS;
  832.     else {
  833.       if(nHostType==HOST_TYPE_AUTO) 
  834.        {
  835.         if(strstr(szMsgBuf,"NCSA")!=NULL ||
  836.            strstr(szMsgBuf,"CUTCP")!=NULL)
  837.           nHostType=HOST_TYPE_NCSA;
  838.         else if(strncmp(szMsgBuf,"214-PC FTP server",17)==0 ||
  839.               strstr(szMsgBuf,"QVT")!=NULL)
  840.           nHostType=HOST_TYPE_QVT;
  841.         else
  842.           {
  843.            defaulttype = TRUE;
  844.            nHostType=HOST_TYPE_UNIX;
  845.           }
  846.       }       
  847.       while((iRetCode!=421) && ((iRetCode/100)!=2 || szMsgBuf[3]=='-'))
  848.        {
  849.         if(strstr(szMsgBuf,"MKD")!=NULL) bCanMKD=TRUE;
  850.         if(strstr(szMsgBuf,"RMD")!=NULL) bCanRMD=TRUE;
  851.         if(strstr(szMsgBuf,"RNFR")!=NULL) bCanREN=TRUE;
  852.         if(strstr(szMsgBuf,"DELE")!=NULL) bCanDELE=TRUE;
  853.         iRetCode=ReadLine(ctrl_socket);
  854.       }
  855.     }
  856.   // if the host type defaulted to unix then try syst instead
  857.   if (defaulttype)
  858.     {
  859.                          
  860.       if(SendPacket(ctrl_socket,"SYST")!=-1) 
  861.         {
  862.           DoPrintf("%s","SYST");
  863.           iRetCode=ReadLine(ctrl_socket);
  864.           DoPrintf("%s",szMsgBuf);
  865.  
  866.           if(strstr(szMsgBuf,"Windows_NT")!=NULL)
  867.             nHostType=HOST_TYPE_WINDOWS_NT;
  868.           else if(strstr(szMsgBuf,"UNIX") !=NULL)
  869.                  nHostType=HOST_TYPE_UNIX;
  870.             else if(strstr(szMsgBuf,"MultiNet") !=NULL)
  871.                  nHostType=HOST_TYPE_VMS_MULTINET;
  872.           else if(strstr(szMsgBuf,"MultiNet") !=NULL)
  873.                  nHostType=HOST_TYPE_VMS_MULTINET;
  874.           else if(strstr(szMsgBuf,"VMS") !=NULL)
  875.                  nHostType=HOST_TYPE_VMS_UCX;
  876.        // add others later as we find out the type
  877.       }
  878.     }
  879.   }
  880.  
  881.    }
  882.  else // host type is  solaris so hardcode stuff
  883.   {
  884.      
  885.         bCanMKD=TRUE;
  886.         bCanRMD=TRUE;
  887.         bCanREN=TRUE;
  888.         bCanDELE=TRUE;
  889.         iRetCode=214;
  890.    }
  891.     
  892.   EnableWindow(hBtnRMKD,bCanMKD);
  893.   EnableWindow(hBtnRRMD,bCanRMD);
  894.   EnableWindow(hBtnRREN,bCanREN);
  895.   EnableWindow(hBtnRDEL,bCanDELE);
  896.   bHELP=TRUE;
  897.   DoPrintf("Host type (%u): %s",nHostType-6000,szHostTypes[nHostType-6000]);
  898.   
  899.  
  900.   return iRetCode;
  901. }
  902.  
  903. //*************************************************//
  904. // routines to load and save our list of hostnames //
  905. //*************************************************//
  906. LPSTR cfgname ="_config_";
  907.  
  908. int GPPS(LPSTR fldname,LPSTR deflt,LPSTR destination,int len) {
  909.   return(GetPrivateProfileString(cfgname,fldname,
  910.               deflt,destination,len,szIniFile));
  911. }
  912.  
  913. int WPPS(LPSTR fldname,LPSTR value) {
  914.   return (WritePrivateProfileString(cfgname,fldname,value,szIniFile));
  915. }
  916.  
  917. void LoadConfigNames(HWND hWndDlg) {
  918.   FILE *fp;
  919.   char szPathName[156];
  920.   char *s;
  921.  
  922.   szPathName[0]=0;
  923.   if(GetWindowsDirectory(szPathName,144)!=0)
  924.     lstrcat(szPathName,"\\");
  925.   lstrcat(szPathName,szIniFile);
  926.   if((fp=fopen(szPathName,"r"))!=NULL) {
  927.     while(fgets(szPathName,150,fp)!=NULL)
  928.       if(szPathName[0]=='[') {
  929.         if((s=strchr(szPathName,']'))!=NULL) *s=0;
  930.         if(stricmp(&szPathName[1],cfgname)!=0)
  931.           SendDlgItemMessage(hWndDlg,DLG_HOST_NAME,CB_ADDSTRING,0,
  932.             (LONG)(LPSTR)&szPathName[1]);
  933.       }
  934.     fclose(fp);
  935.   }
  936. }
  937.             
  938. void LoadUserInfo()
  939. {
  940.   UINT flags;
  941. //  int nIndex;
  942.  // LPSTR s;
  943.  
  944.   szSessionName[0]=0;
  945.   szMailAddress[0]=0;
  946.   szViewer[0]=0;
  947.   szUserID[0]=0;
  948.   szPassWord[0]=0;
  949.   szInitDir[0]=0;
  950.   nHostType=HOST_TYPE_AUTO;
  951.   uiTimeOut=65;
  952.  
  953.   GPPS("SESSION", NULL,      szSessionName, 79);
  954.   GPPS("MAILADDR",NULL,      szMailAddress,127);
  955.   GPPS("VIEWER",  "notepad", szViewer,     120);
  956.   bAutoStart=GetPrivateProfileInt(cfgname, "AUTOSTART",
  957.            bAutoStart,szIniFile);
  958.   flags=GetPrivateProfileInt(cfgname,"FLAGS",64+4+1,szIniFile);
  959.   if(flags & 1) bRecvUniq=1; else bRecvUniq=0;
  960.   if(flags & 2) bStorUniq=1; else bStorUniq=2;
  961.   if(flags & 4) bBell=1; else bBell=0;
  962.   if(flags & 8) bInteractive=1; else bInteractive=0;
  963.   if(flags & 16) bVerbose=1; else bVerbose=0;
  964.   if(flags & 32) bHash=1; else bHash=2;
  965. //if(flags & 64) bSendPort=1; else bSendPort=0;
  966.   if(flags & 128) bDoGlob=1; else bDoGlob=2;
  967. }
  968.  
  969. void SaveUserInfo()
  970. {
  971.   UINT flags;
  972.  // int nIndex;
  973.  
  974.   WPPS(NULL, NULL);
  975.   WPPS("SESSION",  szSessionName);
  976.   WPPS("MAILADDR", szMailAddress);
  977.   WPPS("VIEWER",   szViewer);
  978.   wsprintf(szString,"%u",bAutoStart);
  979.   WPPS("AUTOSTART",szString);
  980.   flags=((bRecvUniq==1)?1:0) +
  981. //  ((bStorUniq==1)?2:0) +
  982.     ((bBell==1)?4:0) +
  983.     ((bInteractive==1)?8:0)+
  984. //  ((bHash==1)?32:0) +
  985. //  ((bSendPort==1)?64:0) +
  986. //  ((bDoGlob==1)?128:0) +
  987.     ((bVerbose==1)?16:0);
  988.   wsprintf(szString,"%u",flags);
  989.   WPPS("FLAGS",szString);
  990. }
  991.  
  992.